from app import create_app, db
from app.models import User, WorkflowTemplate, FormDefinition, WorkflowInstance, WorkflowTask, WorkflowComment, WorkflowHistory

app = create_app()

@app.cli.command('init-db')
def init_db():
    """初始化数据库"""
    db.create_all()
    
    # 创建测试用户
    if not User.query.filter_by(username='admin').first():
        admin = User(username='admin', role='admin')
        admin.set_password('admin123')
        db.session.add(admin)
        
        manager = User(username='manager', role='manager', department='IT')
        manager.set_password('manager123')
        db.session.add(manager)
        
        hr = User(username='hr', role='hr')
        hr.set_password('hr123')
        db.session.add(hr)
        
        employee = User(username='employee', role='employee', department='IT')
        employee.set_password('employee123')
        db.session.add(employee)
        
        db.session.commit()
        print('数据库初始化完成！')

if __name__ == '__main__':
    with app.app_context():
        db.create_all()  # 确保所有表被创建
    app.run(debug=True) 